//
//  MessageCenterViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/14.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "MessageCenterViewController.h"
#import "RemindViewController.h"
#import "AnnouncementViewController.h"
#import "OrderGoodsViewController.h"
#import "RealNameViewController.h"
#import "SMPagerTabView.h"
#import "UserList.h"
#import "MessageCenterStore.h"

@interface MessageCenterViewController ()<SMPagerTabViewDelegate>

@property (nonatomic, strong) NSMutableArray *vcArr;
@property (nonatomic, strong) SMPagerTabView *segmentView;

@end

@implementation MessageCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBarWithTitle:@"消息中心"];
    
    [self setUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setReadPoint:) name:@"HADNOREADMSG" object:nil];
        
}

- (void)setReadPoint:(NSNotification *)noti{
 
    NSString *str = noti.userInfo[@"info"];
    
    NSInteger a = [str integerValue];
    
    [self.segmentView showRedDotWithIndex:a];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setUI{
    
    _vcArr = [NSMutableArray array];
    
    RemindViewController *vc1 = [[RemindViewController alloc] init];
    vc1.title = @"提醒";
    
    AnnouncementViewController *vc2 = [[AnnouncementViewController alloc]initWithNibName:nil bundle:nil];
    vc2.title = @"公告";
    
    OrderGoodsViewController *vc3 = [[OrderGoodsViewController alloc]initWithNibName:nil bundle:nil];
    vc3.title = @"订货";
    
    RealNameViewController *vc4 = [[RealNameViewController alloc]initWithNibName:nil bundle:nil];
    vc4.title = @"实名";
    
    [_vcArr addObject:vc1];
    [_vcArr addObject:vc2];
    [_vcArr addObject:vc3];
    [_vcArr addObject:vc4];
    
    
    self.segmentView.delegate = self;
    
    //可自定义背景色和tab button的文字颜色等
    //开始构建UI
    [_segmentView buildUI];
    
    //起始选择一个tab
    [_segmentView selectTabWithIndex:0 animate:NO];
    
}

#pragma mark - DBPagerTabView Delegate
- (NSUInteger)numberOfPagers:(SMPagerTabView *)view {
    return [_vcArr count];
}
- (UIViewController *)pagerViewOfPagers:(SMPagerTabView *)view indexOfPagers:(NSUInteger)number {
    return _vcArr[number];
}

- (void)whenSelectOnPager:(NSUInteger)number {
    NSLog(@"页面 %lu",(unsigned long)number);
    
    //清除小红点
    MessageCenterStore *store = [[MessageCenterStore alloc] init];
    
    NSString *type = [NSString stringWithFormat:@"%lu",(unsigned long)number + 1];
    
    if ([type isEqualToString:@"1"]) {
        
        return;
    }
    
    [store getMessageInfoWithCustomId:[UserDefaultsManager sharedInstance].user_id andType:type andIsRead:YES Success:^(NSArray *arr, BOOL isRead) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"REALNAMEING" object:nil];
        
    } Failure:^(NSError *error) {
        
    }];
}

#pragma mark - setter/getter
- (SMPagerTabView *)segmentView {
    if (!_segmentView) {
        self.segmentView = [[SMPagerTabView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - (iPhoneX_Top))];
        [self.view addSubview:_segmentView];
    }
    return _segmentView;
}


@end
